package yl.hs.bmipfull.service.dataCockpit.impl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import yl.hs.bmipfull.daos.production.IProductionRecordDao;
import yl.hs.bmipfull.daos.production.IProductionTaskDao;
import yl.hs.bmipfull.service.dataCockpit.IProductionCockpitService;
import yl.hs.bmipfull.utils.CK;
import yl.hs.bmipfull.utils.HsApp;
import yl.hs.bmipfull.viewmodels.dataCockpit.ProductOutputModel;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author 谢东
 * @apiNote
 * @since 2022/8/30 15:25 创建
 */
@Service
public class ProductionCockpitServiceImpl implements IProductionCockpitService {
    @Autowired
    IProductionTaskDao taskDao;

    @Autowired
    IProductionRecordDao recordDao;





    public List<String> product() {
        List<String> list = new ArrayList<>();
        list.add("砂浆");
        list.add("机制砂");
        list.add("商砼");
        list.add("破碎");
        list.add("沥青");
        list.add("稳定土");
        return list;
    }

    public List<Object> WanShan2(List<String> list, List<String> list1) {
        List<Object> list2 = new ArrayList<>();

        var g = list.stream().map(Double::valueOf).collect(Collectors.toList());
        var g1 = list1.stream().map(Double::valueOf).collect(Collectors.toList());
        for (int i = 0; i < g.size(); i++) {
            if (g.get(i) != 0 && g1.get(i) != 0) {
                double v = (double) g.get(i) / g1.get(i);
                list2.add(v);
            }
            list2.add(0);
        }
        return list2;

    }
    @Override
    public Map<String, Object> getActualProduction(String timeType) {
        List<String> actualList = new ArrayList<>();
        List<String> plannedOutputList1 = new ArrayList<>();
        Map<String, Object> map = new HashMap<>();
        String actual = null;
        String plannedOutput = null;
        for (String product : product()) {

            actual = recordDao.getActualProduction(product, timeType);
            plannedOutput = taskDao.getPlannedOutputSum(product, timeType);
            actualList.add(actual);
            plannedOutputList1.add(plannedOutput);
        }
        map.put("category", product());
        map.put("actualData", actualList);
        map.put("plannedData", plannedOutputList1);
        map.put("rateData", WanShan2(actualList, plannedOutputList1));
        return map;
    }


    /**
     *
     *
     * @param timeType
     */
    @Override
    public HashMap<String, Object> getRingPeriod(String timeType) {
        HashMap<String, Object> response = new HashMap<>();
        HashMap<String, Object> period = new HashMap<>();
        List<String> actualList = new ArrayList<>();
        List<String> plannedOutputList1 = new ArrayList<>();
        String actual = null;
        String plannedOutput = null;
        var actualProduction = getActualProduction(timeType);
        for (String product : product()) {
                 // 环期的计划产量
                 actual = taskDao.getRingPeriod(product, timeType);
                 //环期实际总产量
                 plannedOutput = recordDao.getPreviousCycleOfProductOutput(product, timeType);
                actualList.add(actual);
                plannedOutputList1.add(plannedOutput);
        }
        period.put("category", product());
        period.put("actualData", actualList);
        period.put("plannedData", plannedOutputList1);

        response.put("current",actualProduction);
        response.put("RingPeriod",period);
        return response;

    }




    @Override
    public List<ProductOutputModel> getProportionEachProduct(String product, String timeType) {

        return recordDao.getProportionEachProduct(product, timeType);

    }


}
